<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/8
 * Time: 上午2:04
 */

namespace Home\Controller;
use Think\Controller;

class LoginController extends Controller {

    public function index(){
        $referer = $_SERVER['HTTP_REFERER'];
        $this->assign('referer',$referer);
        $this->display();
    }

    //登陆页面
    public function login(){
        if(IS_POST){
            $phone = $_POST['phone'];
            $passwd = $_POST['passwd'];
            if(NULL != $phone && NULL != $passwd){
                $user_db = D('User');
                $user_info = $user_db->getByPhone($phone);
                if($passwd == $user_info['passwd'] && 2!= $user_info['status'] ){
                    session('loginUser',$user_info);
                    //dump($_SESSION);
                    //user_ext信息录入-等级
                    $miss_user_relate_db = D('MissUserRelate');
                    $level_y = $miss_user_relate_db->getLevelY($user_info['user_id']);
                    //dump($level_y);
                    $mission_db = D('Mission');
                    $level_j = $mission_db->getLevelJ($user_info['user_id']);
                    //dump($level_j);
                    $user_ext_db = D('UserExt');
                    $level = $user_ext_db->setLevel($user_info['user_id'],$level_j,$level_y);
                    //dump($level);

                    //获取有无未读消息
                    $message_db = D("Message");
                    $message_result = $message_db->getIsRead($_SESSION['loginUser']['user_id']);
                    if(is_array($message_result)){
                        session('is_read',1);
                    }else{
                        session('is_read',2);
                    }

                    //user_status表信息录入
                    $user_status_db = D('User_status');
                    $data['user_id'] = $user_info['user_id'];
                    ini_set('date.timezone','Asia/Shanghai');
                    $data['login_time'] = date('Y-m-d H:i:s',time());
                    $data['logout_time'] = date("Y-m-d H:i:s",strtotime("+20minutes",strtotime($data['login_time'])));
                    $data['session_id'] = session_id();

                    $result = $user_status_db->add($data);
                    //dump($result);
                    //返回之前页面
                    if( $_POST['referer'] == NULL ){
                        $referer = $_SERVER['HTTP_HOST'].U('Fetcher/index');
                    }else{
                        $referer = $_POST['referer'];
                    }
                    $referer_arr = explode($_SERVER['HTTP_HOST'],$referer);
                    $gourl = $referer_arr[1];
                    $url_arr = explode("/",$gourl);
                    if($url_arr[count($url_arr)-1] == "logout" || $url_arr[count($url_arr)-1] == "iforgot" ){
                        $gourl = U('Fetcher/index');
                    }
                    //dump($gourl);
                    if($result){
                        $this->success("登陆成功!",$gourl);
                    }else{
                        $this->error('状态存入失败!');
                    }

                }else{
                    $this->error("登陆失败!");
                }
            }else{
                $this->error("手机号或者密码为空!");
            }
        }else{
            $this->display('Login');
        }
    }
    public function loginPC(){
        if(IS_POST){
            $phone = $_POST['phone'];
            $passwd = $_POST['passwd'];
            if(NULL != $phone && NULL != $passwd){
                $user_db = D('User');
                $user_info = $user_db->getByPhone($phone);
                if($passwd == $user_info['passwd'] && 2!= $user_info['status'] ){
                    session('loginUser',$user_info);
                    //dump($_SESSION);
                    $user_status_db = D('User_status');
                    $data['user_id'] = $user_info['user_id'];
                    ini_set('date.timezone','Asia/Shanghai');
                    $data['login_time'] = date('Y-m-d H:i:s',time());
                    $data['logout_time'] = date("Y-m-d H:i:s",strtotime("+10minutes",strtotime($data['login_time'])));
                    $data['session_id'] = session_id();

                    $result = $user_status_db->add($data);
                    //dump($result);
                    if($result){
                        $this->success("登陆成功!");
                    }else{
                        $this->error('状态存入失败!');
                    }
                }else{
                    $this->error("登陆失败!");
                }
            }else{
                $this->error("手机号或者密码为空!");
            }
        }else{
            $this->theme('PC')->display('login');
        }
    }


    public function logout(){
        if(IS_GET){
            ini_set('date.timezone','Asia/Shanghai');
            $data['logout_time'] = date('Y-m-d H:i:s',time());
            $user_status_db = D('User_status');
            $user_status_map['session_id'] = array('eq',session_id());
            $user_status_result = $user_status_db->where($user_status_map)->save($data);
            session('is_read',2);
            unset($_SESSION['loginUser']);

            $this->display('index');
        }else{
            $this->error('错误的方法');
        }
    }

    public function logoutPC(){
        if(IS_GET){
            unset($_SESSION['loginUser']);
            $this->theme('PC')->display('login');
        }else{
            $this->error('错误的方法');
        }
    }

    public function iforgot() {
        $this->display();
    }

    //重置密码pc
    public function resetPasswdPC() {
        $this->theme('PC')->display('resetPasswd');
    }

    //重置密码
    public function resetPasswd(){
        if(IS_POST){
            $phone = $_POST['phone'];
            $passwd = $_POST['passwd'];
            $verfiCode = $_POST['verfiCode'];

            if( NULL == $phone || NULL == $passwd || NULL == $verfiCode ){
                $this->error('提交内容不全');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
                $this->error("手机号码错误");
            }
            $user_db = D('User');
            if(!$user_db->isPhoneRegister($phone)){
                $this->error('该手机尚未注册');
            }
            if( $verfiCode != $_SESSION['verfiCode']){
                $this->error('验证码错误');
            }

            $user_result = $user_db->resetPasswd($phone,$passwd);
            if($user_result){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $this->error('错误方法');
        }
    }


    //检验手机验证码
    public function checkVerfiCode(){
        if(IS_POST){
            $verfiCode = $_POST['verfiCode'];
            if( NULL == $verfiCode ){
                $this->error('验证码为空');
            }else if( $verfiCode != $_SESSION['verfiCode']){
                $this->error('验证码错误');
            }else{
                $this->success('验证通过');
            }

        }else{
            $this->error('错误方法');
        }
    }


    //发送手机验证码
    public function sendVerfiCode(){
        $phone = $_GET['phone'];
        $user_db = D('User');
        if(is_null($phone)){
            $this->error("手机号码为空");
        }else if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            $this->error("手机号码错误");
        }else if(!$user_db->isPhoneRegister($phone)){
            $this->error('该手机尚未注册');
        }else{
            //验证码发送URL
            $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $verfiCode = $this->getVerfiCode();
            $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$phone."&content=".rawurlencode("您的验证码是：".$verfiCode."。请不要把验证码泄露给其他人。");
            $result = xml_to_array(post($post_data,$url));
            if($result['SubmitResult']['code'] == 2){
                session('verfiCode',$verfiCode);
                $this->success('验证码已发送');
            }else{
                $this->error('验证码发送失败');
            }
        }
    }

    //生成手机验证码
    public function getVerfiCode(){

        $result = "";
        for($i = 0;$i < 6;$i++){
            $result .= chr(mt_rand(48,57));
        }
        //dump($result);
        return $result;
    }

} 